﻿using AntdUI;
using BST250301;
using BST250301TestStand.Models;
using BST250301TestStand.Models.Utils;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace BST250301.Views
{
    public partial class UserModify : UserControl
    {

        public UserModify()
        {
            InitializeComponent();
            InitData();
        }
        private void InitData()
        {

        }


        private void buttonADD_Click_1(object sender, EventArgs e)
        {
            string old = inputOld.Text;
            string new1 = inputNew.Text;
            string new2 = inputnew2.Text;
            if (string.IsNullOrEmpty(old))
            {
                MessageBox.Show("请输入旧密码");
                return;
            }
            if (string.IsNullOrEmpty(new1))
            {
                MessageBox.Show("请输入新密码");
                return;
            }
            if (string.IsNullOrEmpty(new2))
            {
                MessageBox.Show("请重复新密码");
                return;
            }
            if (new1 != new2)
            {
                MessageBox.Show("两次新密码不一致");
                return;
            }
            //if (!BCrypt.Net.BCrypt.Verify(old, userCurrent.Password))
            //{
            //    MessageBox.Show("当前用户旧密码输入错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}
            //string hashedPassword = BCrypt.Net.BCrypt.HashPassword(new1);
            var userCurrent = DB.GetUserData(MainWindow.configapp.DBConnectionServer, MainWindow.userCurrent.Account);
            if (userCurrent.Account == MainWindow.userCurrent.Account && userCurrent.Password == old)
            {
                int ret = DB.SetUserData(MainWindow.userCurrent.Account, new1);
                if (ret > 0)
                {
                    MessageBox.Show($"成功修改密码");
                    System.Threading.Thread.Sleep(10);
                    Process.GetCurrentProcess().Kill();
                    Program.LoginWindow.Show();
                }
                else
                    MessageBox.Show($"未能成功修改密码");
            }
            else
            {
                MessageBox.Show($"用户无权修改密码");
            }
        }

    }
}
